﻿using BootyBay.StoreAppMgmtCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;


namespace BootyBay.StoreAppMgmtCore
{
    public class StoreAppInitializer : AgentSessionBase
    {
        /// <summary>
        /// 
        /// </summary>
        public void StartInstallStoreApp()
        {
         

            AppMgr appOperationMgr = new AppMgr();
            
            //PrintMsg("Checking installation Of BootyBayStoreApp...");

            //appOperationMgr.OnNotificationOutput += appOperationMgr_OnNotificationOutput;

            ////TODO: move to appconfig file or refer from webservice from hardcoded value;
            //string storeAppPackageName = "4eee5a62-26ee-4c1f-ab12-64ef8a765cfa";
            //string storeAppPublisherName = @"CN=""Yu, Renjie"", O=""Yu, Renjie"", L=Shanghai, S=Shanghai, C=CN";
            //string lastestVersion = "1.0.0.2";
            //string pkgLocation = "https://bootybay.blob.core.windows.net/bootybaystore/BootyBay.PortalApp_1.0.0.2_AnyCPU.appx";
            //string pkgCertLocation = "https://bootybay.blob.core.windows.net/bootybaystore/BootyBay.PortalApp_1.0.0.2_AnyCPU.cer";

            //var localPackage = appOperationMgr.FindPackage(storeAppPackageName, storeAppPublisherName);

 

            //if (localPackage == null || localPackage.AppPackageVersion != lastestVersion)
            //{
            //    appOperationMgr.InstallCert(pkgCertLocation);
            //    appOperationMgr.AddPackage(pkgLocation);

            //    localPackage = appOperationMgr.FindPackage(storeAppPackageName, storeAppPublisherName);
            //}

            //PrintMsg("BootyBayStoreApp has been installed successfully.");

            //hardcoded, from BootyBay.PrivateStoreApp / Package.appxmanifest. 
            //TODO - should retrive the info from server. We removed the reference from the agent to the server for now Jan.24th.
            var familyName = "0387ce2a-2c87-4ca7-9dbd-a557f8852404_c34mdgxg2yanp"; //localPackage.AppPackageFamilyName;

            
            PrintMsg("Enabling LoopbackExempt For " + familyName);

            string retcode = appOperationMgr.EnableLoopbackExemptForApp(familyName);

            PrintMsg("LoopbackExempt done. return code: " + retcode);


            //Get-ItemProperty -Path hkcu:\Software\Microsoft\Windows\CurrentVersion\AccountPicture |  fl *AppsReadAccess

            PrintMsg("Enabling AppsReadAccess..");

            PowerShell enableUserNameAccessPS = PowerShell.Create();

            enableUserNameAccessPS.AddCommand("Set-ItemProperty");

            //Set-ItemProperty -Path hkcu:\Software\Microsoft\Windows\CurrentVersion\AccountPicture -Name AppsReadAccess -Value 0
            enableUserNameAccessPS.AddParameter("Path",@"hkcu:\Software\Microsoft\Windows\CurrentVersion\AccountPicture");
            enableUserNameAccessPS.AddParameter("Name","AppsReadAccess");
            enableUserNameAccessPS.AddParameter("Value", "1");

            var output = enableUserNameAccessPS.Invoke();
            
            PrintMsg("AppsReadAccess enabled.");

            //PrintMsg("Start to run the store app...");

            //string url = "bootybay:";

            //System.Diagnostics.Process.Start(url);

           
        }



        void appOperationMgr_OnNotificationOutput(object sender, AppMgmtEventArgs args)
        {
            PrintMsg(args.EventMessage);
        }
    }
}
